/**
 * 详情对话框
 */
var RedlineInfoDlg = {
    data: {
        id: "",
        name: "",
        mianPic: "",
        ms: "",
        phone: "",
        tags: "",
        jiage: "",
        pics: "",
        jieshao: "",
        xuzhi: "",
        isdel: "",
        createTime: "",
        uid: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects','tagsInput'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    var tagsInput = layui.tagsInput;

    var upload = layui.upload;
    var upload2 = layui.upload;


    //多图片上传
    upload.render({
        elem: '#mainpic'
        ,url: Feng.ctxPath + '/system/uploadFtp' //此处配置你自己的上传接口即可
        ,multiple: false
        ,done: function(res){
            $('#mainpicview').html('<img src="'+ res.data +'" alt="" class="layui-upload-img">');
            $('#mianPic').val(res.data);
            console.log(res)
        }
    });

    upload2.render({
        elem: '#picsbtn'
        ,url: Feng.ctxPath + '/system/uploadFtp' //此处配置你自己的上传接口即可
        ,multiple: true
        ,done: function(res){

            var s = $("#pics").val()+''

            console.log(s)

            var ss = []
            if(s.length>0){
                ss = s.split(',');
                ss.push(res.data)
                $("#pics").val(ss)
            }else{
                $("#pics").val(res.data)
            }

            $('#picsview').append('<img src="'+ res.data +'" alt="" data-src="'+res.data+'" class="layui-upload-img">');
        }
    });



    //实例化编辑器
    var ue = UE.getEditor('jieshao', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    var ue2 = UE.getEditor('xuzhi', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/ueditor/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };




    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/redline/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('redlineForm', result.data);

    let mainpic = result.data.mianPic+''
    let pics = result.data.pics+''

    if(mainpic.length>0){
        $('#mainpicview').html('<img src="'+ mainpic +'" alt="" data-src="'+mainpic+'" class="layui-upload-img">');
    }

    if(pics.length>0){
        let ss = pics.split(',')
        for(var i=0;i<ss.length;i++){
            $('#picsview').append('<img src="'+ ss[i] +'" alt="" data-src="'+ss[i]+'" class="layui-upload-img">');
        }
    }


    // BackSpace键可删除标签
    $('#tags').tagsInput({removeWithBackspace: true});

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/redline/editItem", function (data) {
            Feng.success("更新成功！");
            window.location.href = Feng.ctxPath + '/redline'
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#cancel').click(function(){
        window.location.href = Feng.ctxPath + '/redline'
    });
});